package com.zyf.ppx.ui

import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.bumptech.glide.Glide
import com.mylhyl.superdialog.SuperDialog
import com.wingsofts.dragphotoview.DragPhotoView
import com.zyf.ppx.R
import com.zyf.ppx.bean.MuitImage
import com.zyf.ppx.download_utils.DownloadUtils
import kotlinx.android.synthetic.main.activity_image.*


class ImageActivity : AppCompatActivity() {
    var pos = 0
    var data = ArrayList<MuitImage>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        setContentView(R.layout.activity_image)
        initView()
    }

    private fun initView() {
        back.setOnClickListener { finish() }
        pos = intent.getIntExtra("pos",0)
        data = intent.getSerializableExtra("data") as ArrayList<MuitImage>
        image_vp.adapter = object : PagerAdapter(){
            override fun isViewFromObject(view: View, view1: Any): Boolean {
                return view === view1
            }

            override fun getCount(): Int {
                return data.size
            }

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                var img = DragPhotoView(applicationContext)
                val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                img.layoutParams = params
                Glide.with(this@ImageActivity).load(data[position].download_list[0].url).into(img)
                img.setOnExitListener{ _, _, _, _, _ ->
                    finish()
                }
                img.setOnLongClickListener{
                    val list = ArrayList<String>()
                    list.add("保存图片")
                    SuperDialog.Builder(this@ImageActivity)
                        .setCanceledOnTouchOutside(true)
                        .setItems(list) {
                            DownloadUtils.download(data[position],this@ImageActivity)
                        }
                        .setNegativeButton("取消", null)
                        .build()
                    return@setOnLongClickListener true
                }
                container.addView(img)
                return img
            }

            override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
                container.removeView(view as View)
            }
        }
    }
}
